#include "ui.h"
#include "disp.h"
#include "wifi.h"
#include "rtc.h"
#include "sht.h"
#include "bitmap.h"
#include "string.h"
#include "weather.h"
#include "bs818.h"

uint8_t ad_enable = 1;
uint8_t ui_now = 0;
uint8_t font_color[3] = {200, 26, 116};
const uint32_t* weather_icon[39] = {
    img_qing_day, img_qing_night, img_qing_day, img_qing_night, img_duoyun, img_duoyun_day, img_duoyun_night,
    img_duoyun_day, img_duoyun_night, img_yin, img_zhenyu, img_leizhenyu, img_leibingbao, img_xiaoyu, img_zhongyu,
    img_dayu, img_baoyu, img_dabaoyu, img_tedabaoyu, img_dongyu, img_yujiaxue, img_zhenxue, img_xiaoxue, img_zhongxue, 
    img_daxue, img_baoxue, 
};

uint8_t move_cnt = 0, move_cnt2 = 0;
int8_t move_y1 = 0;

void endMove(void)
{
    move_cnt = 0;
    move_cnt2 = 0;
    move_y1 = 0;
}

static uint8_t calStrLength(int8_t a)
{
    if(a>=0 && a<10)
        return 3;
    else if(a<=-10)
        return 11;
    else
        return 7;
    return 0;
}

void weatherTempDisp(void)
{
    uint8_t weight = 0, disp_pos_x = 0;


    char temp_str[30];
    memset(temp_str, 0, 30);
    if(weather_img != NULL)
    {
        dispClear();
        

        setColor(0, 0xC8, 0xC8);
        sprintf(temp_str, "%s\"~%s\"", weather_temp_night.c_str(), weather_temp_day.c_str());

        weight = calStrLength(weather_temp_night.toInt());
        weight += calStrLength(weather_temp_day.toInt());
        weight += 11;
        disp_pos_x = (32-weight)/2;

        drawString(temp_str, disp_pos_x, -6+move_y1);

        drawBitmapRGB(weather_icon[weather_code], 2, move_y1, 8, 8);

        if(weather_temp.toInt() < 18)
            setColor(0, 0, 200);
        else if(weather_temp.toInt() >= 18 && weather_temp.toInt() <= 25)
            setColor(0, 200, 0);
        else
            setColor(200, 0, 0);


        memset(temp_str, 0, 30);
        sprintf(temp_str, "%s\"", weather_temp.c_str());

        weight = calStrLength(weather_temp.toInt());
        disp_pos_x = (22-weight)/2;
        drawString(temp_str, 10+disp_pos_x, 2+move_y1);

        if(move_cnt>140)
        {
            if(move_cnt2 == 2)
            {
                move_cnt2 = 0;
                move_y1++;
            }
            if(move_y1 >= 8)
                move_y1 = 8;
            move_cnt2++;
        }
        move_cnt++;
    }
}

void timeDisp(void)
{
    static uint16_t cnt = 0, trans_time = 0, trans_n = 0, eye_pos = 0;
    uint8_t i = 0;
    char time_str[20];

    dispClear();
    drawBitmapRGB(img_douren, 1, 1, 8, 6);
    if(cnt >= trans_n)
    {
        cnt = 0;
        eye_pos = random(1, 6);
        trans_time = random(1, 5);
        trans_n = 100 * trans_time;
    }
    switch (eye_pos)
    {
    case 1:
        setColor(200, 200, 200);
        drawPoint(3, 3, 0);
        drawPoint(4, 3, 1);
        drawPoint(6, 3, 0);
        drawPoint(7, 3, 1);
        drawPoint(3, 5, 1);
        drawPoint(4, 5, 1);
        drawPoint(6, 5, 1);
        drawPoint(7, 5, 1);
        drawPoint(3, 4, 1);
        drawPoint(4, 4, 1);
        drawPoint(6, 4, 1);
        drawPoint(7, 4, 1);
        break;
    case 2:
        setColor(200, 200, 200);
        drawPoint(3, 3, 1);
        drawPoint(4, 3, 0);
        drawPoint(6, 3, 1);
        drawPoint(7, 3, 0);
        drawPoint(3, 5, 1);
        drawPoint(4, 5, 1);
        drawPoint(6, 5, 1);
        drawPoint(7, 5, 1);
        drawPoint(3, 4, 1);
        drawPoint(4, 4, 1);
        drawPoint(6, 4, 1);
        drawPoint(7, 4, 1);
        break;
    case 3:
        setColor(200, 200, 200);
        drawPoint(3, 3, 1);
        drawPoint(4, 3, 1);
        drawPoint(6, 3, 1);
        drawPoint(7, 3, 1);
        drawPoint(3, 5, 1);
        drawPoint(4, 5, 0);
        drawPoint(6, 5, 1);
        drawPoint(7, 5, 0);
        drawPoint(3, 4, 1);
        drawPoint(4, 4, 1);
        drawPoint(6, 4, 1);
        drawPoint(7, 4, 1);
        break;
    case 4:
        setColor(200, 200, 200);
        drawPoint(3, 3, 1);
        drawPoint(4, 3, 1);
        drawPoint(6, 3, 1);
        drawPoint(7, 3, 1);
        drawPoint(3, 5, 0);
        drawPoint(4, 5, 1);
        drawPoint(6, 5, 0);
        drawPoint(7, 5, 1);
        drawPoint(3, 4, 1);
        drawPoint(4, 4, 1);
        drawPoint(6, 4, 1);
        drawPoint(7, 4, 1);
        break;        
    case 5:
        setColor(200, 200, 200);
        drawPoint(3, 3, 1);
        drawPoint(4, 3, 1);
        drawPoint(6, 3, 1);
        drawPoint(7, 3, 1);
        drawPoint(3, 5, 1);
        drawPoint(4, 5, 1);
        drawPoint(6, 5, 1);
        drawPoint(7, 5, 1);
        drawPoint(3, 4, 0);
        drawPoint(4, 4, 1);
        drawPoint(6, 4, 0);
        drawPoint(7, 4, 1);
        break;        
    case 6:
        setColor(200, 200, 200);
        drawPoint(3, 3, 1);
        drawPoint(4, 3, 1);
        drawPoint(6, 3, 1);
        drawPoint(7, 3, 1);
        drawPoint(3, 5, 1);
        drawPoint(4, 5, 1);
        drawPoint(6, 5, 1);
        drawPoint(7, 5, 1);
        drawPoint(3, 4, 1);
        drawPoint(4, 4, 0);
        drawPoint(6, 4, 1);
        drawPoint(7, 4, 0);
        break;        
    default:
        break;
    }
    cnt++;

    setColor(font_color[0], font_color[1], font_color[2]);
    memset(time_str, 0, 20);
    sprintf(time_str, "%.2d:%.2d", rtc.hour, rtc.min);
    drawString(time_str, 12, 1);

    for(i=0; i<7; i++)
    {
        if(rtc.week == i)
            setColor(0, 90, 200);
        else
            setColor(0, 200, 90);
        drawPoint(10+i*3, 6, 1);
        drawPoint(11+i*3, 6, 1);
    }

}

void dateDisp(void)
{
    char date_str[20];

    dispClear();
    drawBitmapRGB(rili, 2, 0, 8, 8);
    setColor(font_color[0], font_color[1], font_color[2]);
    memset(date_str, 0, 20);
    sprintf(date_str, "%.2d.%.2d", rtc.month, rtc.date);
    drawString(date_str, 12, 2);
}

void tempDisp(void)
{
    char temp_str[20];

    dispClear();
    drawBitmapRGB(wenduji, 2, 0, 5, 8);
    memset(temp_str, 0, 20);
    sprintf(temp_str, "%d.%.2d\"", sht31.temp/100, sht31.temp%100);

    if(sht31.temp<1800)
        setColor(0, 0, 200);
    else if(sht31.temp>=1800 && sht31.temp<=2500)
        setColor(0, 200, 0);
    else
        setColor(200, 0, 0);
    drawString(temp_str, 10, 2);
}

void humiDisp(void)
{
    char humi_str[20];

    dispClear();
    drawBitmapRGB(shuidi, 2, 1, 5, 6);
    memset(humi_str, 0, 20);
    sprintf(humi_str, "%d.%.2d%%", sht31.humi/100, sht31.humi%100);

    if(sht31.humi < 4000)
        setColor(200, 0, 0);
    else if(sht31.humi>=4000 && sht31.humi<=7000)
        setColor(0, 200, 0);
    else
        setColor(0, 0, 200);
    drawString(humi_str, 8, 2);
}

uint8_t transit(void)
{
    static uint8_t cnt = 0;
    static float current_bright = 0.0f, i = 0.0f;
    float bright = getBrightness();
    if(cnt == 0)
    {
        current_bright = bright;
        i = current_bright/15;
    }

    if(cnt == 2)
    {
        cnt = 0;
        bright -= i;
    }
    cnt++;
    if(bright <= 0.05)
    {
        cnt = 0;
        return 1;
    }
    setBrightness(bright);
    return 0;
}

void analogTask(void)
{
    static uint16_t cnt = 0;
    uint16_t ad_value = 0;
    if(ad_enable)
    {
        ad_value = analogRead(A0);    
        if(ad_value < 30)
        {
            cnt++;
            if(cnt >= 50 && (rtc.hour>=0 && rtc.hour<8 || rtc.hour==23))
                setBrightness(0);
            else
                setBrightness((float)normal_bright/200.0);
        }
        else
        {
            cnt = 0;
            setBrightness((float)normal_bright/100.0);
        }
        // Serial.printf("AD:%d\r\n", ad_value);
    }
}

void uiTask(void)
{
    static uint8_t brightcach_n = 0;
    static uint16_t cnt = 0;
    uint16_t tick = 0;
    static uint16_t _tick = 0;
    static float bright_cach = 0;
    static uint8_t menu_cnt = 0;

#if 1
    if(key_status == KEY_LEFT)     
    {
        key_status = KEY_NONE;
        if(ui_now == 0)
            ui_now = 4;
        else
            ui_now--;
        cnt = 0;
        _tick = 200;     //disp one second longer than auto
    }
    else if(key_status == KEY_RIGHT)
    {
        key_status = KEY_NONE;
        ui_now++;
        if(ui_now > 4)
            ui_now = 0;
        cnt = 0;
        _tick = 200;
    }

    if(ui_now == 0)
        tick = 1000;
    else if(ui_now==1 && weather_img!=NULL)
        tick = 300;
    else
        tick = 100 + _tick;
    cnt++;
    if(cnt >= tick)
    {
        _tick = 0;
        ad_enable = 0;
        if(brightcach_n == 0)
        {
            brightcach_n = 1;
            bright_cach = getBrightness();
        }
        if(transit())
        {
            ad_enable = 1;
            brightcach_n = 0;
            cnt = 0;
            ui_now++;
            if(ui_now == 5)
                ui_now = 0;
            setBrightness(bright_cach);
        }
    }
    switch(ui_now)
    {
    case 0:
        timeDisp();
        break;
    case 1:
        if(weather_img != NULL)
            weatherTempDisp();
        else
            ui_now++;
        break;
    case 2:
        endMove();
        dateDisp();
        break;
    case 3:
        tempDisp();
        break;
    case 4:
        humiDisp();
        break;
    default:
        break;
    }
#else
    drawBitmapRGB(img_xiaoxue, 1, 0, 8, 8);
    drawBitmapRGB(img_zhongxue, 10, 0, 8, 8);
    drawBitmapRGB(img_daxue, 19, 0, 8, 8);
#endif
    disp();
}
